package api

import (
	"fmt"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
	"weiboxs.com/new_h_glasses/utils"
)

type ApiArchivesController struct {
	ApiBaseController
}

func (c *ApiArchivesController) Prepare() {
	c.ApiBaseController.Prepare()
	c.CtrLoginToken()
}

//首页
func (c *ApiArchivesController) ApiIndex() {

}

func (c *ApiArchivesController) ApiDataGrid() {

	limit,_ := c.GetInt("limit", 20)
	offset,_ := c.GetInt64("offset", 0)
	filter := c.GetString("keyword")
	var params models.ArchivesQueryParam

	params.Limit = limit
	params.Offset = offset
	params.Filter = filter

	data, total := models.ArchivesPageList(&params)
	c.jsonResultData(enums.JRCodeSucc, total, data)
}

func (c *ApiArchivesController) ApiDelete() {
	id, _ := c.GetInt64("id")
	if err := models.ArchivesDelete(id, c.curUser.DepartmentId); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "删除成功", "")
}

func (c *ApiArchivesController) ApiEdit() {
	//如果是POST请求，则由Save处理
	if c.Ctx.Input.IsPost() {
		c.ApiSave()
	}
	id, _ := c.GetInt64("id", 0)

	m := &models.Archives{}

	if id == 0 {
	} else {
		m, _ = models.GetArchivesById(id)
	}
	c.Data["m"] = m

	c.jsonResult(enums.JRCodeSucc, "", c.Data)
}

func (c *ApiArchivesController) createDocumentNumber() string {
	var documentNumber string = ""
	stat := utils.GetDateParse(time.Now().Format("2006-01-02"))
	end := stat + 86400
	documentNumber = fmt.Sprintf("%s%04d", time.Now().Format("20060102150405"), models.QueryOptometryTimeSoltSeterCount(&c.curUser, stat, end)+1)
	return documentNumber
}

func (c *ApiArchivesController) ApiSave() {

	var err error
	m := models.Archives{}
	//获取form里的值
	if err = c.ParseForm(&m); err != nil {
		c.jsonResult(enums.JRCodeFailed, "获取数据失败", m.Id)
	}

	if m.Id == 0 {
		m.CreatedBy = c.curUser.RealName
		m.UpdatedBy = c.curUser.RealName
		m.DepartmentId = c.curUser.DepartmentId
		if _, err = models.ArchivesAdd(&m); err != nil {
			c.jsonResult(enums.JRCodeFailed, "添加失败", m.Id)
		}

	} else {

		if mr, err := models.GetArchivesById(m.Id); err != nil {
			c.jsonResult(enums.JRCodeFailed, "获取数据失败", m.Id)
		} else {
			m.Status = 1
			m.CreatedAt = mr.CreatedAt
			m.CreatedBy = mr.CreatedBy
			m.DepartmentId = c.curUser.DepartmentId
		}

		m.UpdatedBy = c.curUser.RealName
		if err = m.ArchivesEdit(); err != nil {
			c.jsonResult(enums.JRCodeFailed, "编辑失败", m.Id)
		}
	}

	c.jsonResult(enums.JRCodeSucc, "操作成功", "")
}

func (c *ApiArchivesController) ApiSelectArchives() {
	keyword := c.GetString("keyword")
	var params models.ArchivesQueryParam
	params.Filter = keyword

	if count := models.ArchivesFilterCount(&params); count <= 0 {
		c.jsonResult(enums.JRCodeSucc, "没有重复记录", "")
	} else {
		c.jsonResult(enums.JRCodeFailed, "有重复记录", count)
	}
}

//查看单条档案数据,JSON返回
func (c *ApiArchivesController) ApiViewOne() {

	if c.Ctx.Input.IsPost() {
		arcId, _ := c.GetInt64("id") //档案ID
		data, err := models.GetArchivesById(arcId)
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "获取成功", data)
	}

	c.jsonResult(enums.JRCodeFailed, "获取指定参数出错！", "")
}

//从HIS获取资料
func(c *ApiArchivesController) ApiFindByHis() {
	number := c.GetString("number")
	if len(number) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "就诊卡号不能为空", "")
	}
	ser := new(services.MssqlService)
	screening, err := ser.GetScreeningInfo(number, c.curUser)
	if(err != nil) {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", screening)
}